<template>
  <div class="register-container">
    <div class="register-box">
      <!-- 标题区域 -->
      <div class="register-title">
        <h2>智能大棚监控系统</h2>
        <p class="subtitle">Intelligence Greenhouse Monitoring</p>
      </div>

      <!-- 注册表单 -->
      <div class="register-form">
        <div class="form-header">
          <h3>账号注册</h3>
          <p>欢迎加入！请填写以下信息完成注册。</p>
        </div>

        <el-form 
          ref="registerFormRef"
          :model="registerForm"
          :rules="rules"
          size="large"
        >
          <el-form-item prop="username">
            <el-input
              v-model="registerForm.username"
              placeholder="请输入用户名（至少5位字符）"
              clearable
              class="custom-input"
            >
              <template #prefix>
                <el-icon class="input-icon"><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              v-model="registerForm.password"
              type="password"
              placeholder="请输入密码（至少6位字符）"
              show-password
              clearable
              class="custom-input"
            >
              <template #prefix>
                <el-icon class="input-icon"><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="confirmPassword">
            <el-input
              v-model="registerForm.confirmPassword"
              type="password"
              placeholder="请再次输入密码"
              show-password
              clearable
              class="custom-input"
            >
              <template #prefix>
                <el-icon class="input-icon"><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-button
            type="primary"
            :loading="loading"
            class="submit-btn"
            @click="handleRegister"
          >
            {{ loading ? '注册中...' : '注册' }}
          </el-button>

          <div class="login-tip">
            已有账号？
            <router-link to="/login" class="login-link">
              立即登录
            </router-link>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { User, Lock } from '@element-plus/icons-vue'
import type { FormInstance } from 'element-plus'
import { register } from '../api/user'
import type { RegisterData } from '../api/user'
import { ElMessage } from 'element-plus'

const router = useRouter()
const registerFormRef = ref<FormInstance>()
const loading = ref(false)

interface RegisterForm extends RegisterData {
  confirmPassword: string
}

const registerForm = reactive<RegisterForm>({
  username: '',
  password: '',
  confirmPassword: ''
})

const validatePass2 = (rule: any, value: string, callback: Function) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, message: '用户名长度不能小于5位', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { validator: validatePass2, trigger: 'blur' }
  ]
}

const handleRegister = async () => {
  if (!registerFormRef.value) return
  
  try {
    await registerFormRef.value.validate()
    loading.value = true
    
    const res = await register(registerForm)
    if (res.message === '注册成功') {
      ElMessage.success('注册成功，请登录')
      router.push('/login')
    } else {
      ElMessage.error(res.message || '注册失败')
    }
  } catch (error) {
    console.error('Register error:', error)
    ElMessage.error('注册失败，请重试')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped lang="scss">
// 复用 Login.vue 的大部分样式，只需修改类名
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
}

.register-box {
  width: 480px;
  position: relative;
  z-index: 1;
}

.register-title {
  text-align: center;
  margin-bottom: 40px;
  color: white;
  
  h2 {
    font-size: 32px;
    margin: 0 0 8px;
    font-weight: 600;
    text-shadow: 0 2px 4px rgba(0,0,0,0.1);
  }
  
  .subtitle {
    font-size: 16px;
    margin: 0;
    opacity: 0.9;
    letter-spacing: 1px;
  }
}

.register-form {
  background: #fff;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
}

.form-header {
  text-align: center;
  margin-bottom: 24px;
  
  h3 {
    font-size: 24px;
    color: #333;
    margin: 0 0 8px;
  }
  
  p {
    color: #999;
    font-size: 14px;
    margin: 0;
  }
}

.custom-input {
  :deep(.el-input__wrapper) {
    background: #ffffff;
    box-shadow: none !important;
    border: none;
    border-radius: 4px;
    padding: 0 12px;
    
    &.is-focus {
      background: #ffffff;
    }
  }
  
  :deep(.el-input__inner) {
    height: 40px;
    color: #333333;
    font-size: 14px;
    background: #ffffff;
    
    &::placeholder {
      color: #909399;
    }
  }

  .input-icon {
    font-size: 16px;
    color: #909399;
  }
}

.submit-btn {
  width: 100%;
  height: 40px;
  font-size: 16px;
  background: #409eff;
  border: none;
  margin-top: 12px;
  
  &:hover {
    background: #66b1ff;
  }
}

.login-tip {
  text-align: center;
  margin-top: 20px;
  color: #8c8c8c;
  font-size: 14px;
}

.login-link {
  color: #1890ff;
  text-decoration: none;
  font-weight: 500;
  
  &:hover {
    color: #40a9ff;
  }
}

@media (max-width: 576px) {
  .register-box {
    width: 90%;
    margin: 0 20px;
  }
}
</style> 